package com.cloud.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;

/**
 *describe:
 *
 *@author zhanghaixuan
 *@date 2018/10/09
 **/
@RestController
@RequestMapping("/hystrix")
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
	/**
	 * hystrix 超时配置
	 */
	//	@HystrixCommand(commandProperties = {
//			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
//	})

	/**
	 * hystrix 熔断配置
	 */
	@HystrixCommand(commandProperties = {
			//设置熔断	开启
			@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
			// 当在配置时间窗口内达到此数量的失败后，进行短路。默认20个
			@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
			//设置 10秒内将打开熔断器  如果十秒过后变为半开 之后成功就全部放行
			@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
			//百分比 如果失败到达60% 就会把所有的请求都失败
			@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),
	})
	@GetMapping("/product")
	public String getProductInfoList(){
		RestTemplate restTemplate = new RestTemplate();

		String s = restTemplate
				.postForObject("http://127.0.0.1:8082/product/listForOrder", Arrays.asList("157875196366160022"),
						String.class);

		return s;
	}

	public String fallback(){
		return "太拥挤了,请稍后再试";
	}

	public String defaultFallback(){
		return "太拥挤了,请稍后再试defaultFallback";
	}
}
